dojo.provide("uws.Menu");

//display the Edit Account form
function editAccount(){
	dojo.byId("statusBar").innerHTML='Loading "Edit Account"';
	dojo.require("dijit.form.Form");
	dojo.require("dijit.form.Button");
	dojo.require("dijit.form.ValidationTextBox");
	dojo.require("dojox.widget.Calendar");
	dojo.require("dijit.form.DateTextBox");
	dojo.require("dojox.form.PasswordValidator");
	dojo.addOnLoad(function(){
		var tab;
		if(!dijit.byId('editAccount')){
			tab = new dijit.layout.ContentPane({
				id:'editAccount',
				title:"Edit Account",
				href:'ajax/editaccount.php',
				closable:true,
				preload:true,
				onDownloadEnd:function(){
					dojo.byId("statusBar").innerHTML='Ready...';
					var date = dijit.byId("birthdate");
					var mindate = new Date();
					var maxdate = new Date();
					
					mindate.setFullYear(maxdate.getFullYear()-100);
					maxdate.setFullYear(maxdate.getFullYear()-10);
					
					date.constraints.max=mindate;
					date.constraints.max=maxdate;
				}
			});
			dijit.byId("tabContainer").addChild(tab);
		}else{
			tab=dijit.byId('editAccount');
			tab.set('content', 'Loading...');
			tab.onDownloadEnd=function(){
				dojo.byId("statusBar").innerHTML='Ready...';
				var date = dijit.byId("birthdate");
				var mindate = new Date();
				var maxdate = new Date();
				
				mindate.setFullYear(maxdate.getFullYear()-100);
				maxdate.setFullYear(maxdate.getFullYear()-10);
				
				date.constraints.max=mindate;
				date.constraints.max=maxdate;
				date.setValue(maxdate);
			};
			tab.setHref('ajax/editaccount.php');
		}
		dijit.byId("tabContainer").selectChild(tab);
	});
}

//submit the edit account form
function editAccountSubmit(){
	var form = dijit.byId("editAccountForm");
	if(form.validate()){
		var xhrPostArgs={
			form: dojo.byId("editAccountForm"),
			handleAs: "text",
			load:function(response, ioArgs){
				dijit.byId('editAccount').set('content', response);
				dojo.byId("statusBar").innerHTML='Ready...';
			},
			error:function(response, ioArgs){
				new dijit.Dialog({
					title: "Edit Account",
					style: "width: 300px",
					content: "Edit account failed. "+response
				}).show();
				dojo.byId("statusBar").innerHTML='Ready...';
			}
		};
		dojo.xhrPost(xhrPostArgs);
		dojo.byId("statusBar").innerHTML='Sending request...';
		return false;
	}else{
		return false;
	}
}

//delete the account
function deleteAccount(){
	dojo.xhrGet({
		url:"ajax/deleteaccount.php",
		load:function(response, ioArgs){
			dijit.byId('editAccount').set('content', response);
			dojo.byId("statusBar").innerHTML='Ready...';
		},
		error:function(response, ioArgs){
			new dijit.Dialog({
				title: "Delete Account",
				style: "width: 300px",
				content: "Delete account failed. "+response
			}).show();
			dojo.byId("statusBar").innerHTML='Ready...';
		}
	});
	dojo.byId("statusBar").innerHTML='Sending request...';
}

//create a new project
function newProject(){
	if(dataStore!=null && dataStore.isDirty()){
		if(tree!=null) tree.destroyRecursive();
		closeProject();
	}else if(dataStore!=null){
		closeProject();
	}
	
	var projectName = prompt("Project name","New Project");
	
	if(projectName == null || projectName.length==0){
		return;
	}
	
	writeToStatusBar("Loading...");
	initDataStore("ajax/newproject.json");
	prepareDataStore();	
	
	dataStore.newItem({
		id: 'project',
		name: projectName,
		diag_counter: 0,
		comp_children_counter: 0,
		comp_counter: 0,
		conn_counter: 0,
		dateCreated: '',
		dateModified: '',
		db_key: -1,
		children: []
	});
	buildNavTree();
	
	dojo.subscribe("tabContainer-selectChild", function(child){
		document.title=child.title+" - "+username+" - UML WebStudio";
		surfaceManager.setVisibleSurface(child.id);
	});
	
	dojo.subscribe("tabContainer-removeChild", function(child){
		surfaceManager.removeItem(child.id, true, false);
	});
}

//view projects made by the current user
function viewProjects(){
	var tab;
	
	if(!dijit.byId('viewProjects')){
		tab = new dijit.layout.ContentPane({
			id: "viewProjects",
			title:"Projects",
			href:'ajax/projects.php',
			closable:true,
			preventCache:true
		});
		dijit.byId("tabContainer").addChild(tab);
	}else{
		tab=dijit.byId('viewProjects');
		tab.refresh();
	}
	dijit.byId("tabContainer").selectChild(tab);
}

//offer to save the currently open project and close the project
function closeProject(){
	if(!offerToSave()) return;
	dojo.byId("navigation").innerHTML= ''+
		'<p class="center">'+
			'<a href="javascript:viewProjects();"><span style="font-style:italic">Open a Project</span></a><br />'+
			'<a href="javascript:newProject();"><span style="font-style:italic">Create a Project</span></a>'+
		'</p>';
	dojo.byId("propertiesName").innerHTML = '';
	dojo.byId("propertiesDiv").innerHTML = '<span style="font-style:italic">Please select an item.</span>';
	
	dataStore.fetchItemByIdentity({
		identity: "project",
		onItem: function(item){
			dojo.forEach(dataStore.getValues(item, "children"), function(childItem) {
				surfaceManager.removeItem(dataStore.getValue(childItem, "id"), false, true, true);
			});
		}
	});
	dataStore = null;
	tree.destroyRecursive();
}

//delete the project
function deleteProject(){
	dojo.byId("navigation").innerHTML= ''+
		'<p class="center">'+
			'<a href="javascript:viewProjects();"><span style="font-style:italic">Open a Project</span></a><br />'+
			'<a href="javascript:newProject();"><span style="font-style:italic">Create a Project</span></a>'+
		'</p>';
	dojo.byId("propertiesDiv").innerHTML = '<span style="font-style:italic">Please select an item.</span>';
	
	dataStore.fetchItemByIdentity({
		identity: "project",
		onItem: function(item){
			var dbkey = dataStore.getValue(item,"db_key");
			if(dbkey==-1){
				dojo.forEach(dataStore.getValues(item, "children"), function(childItem) {
					surfaceManager.destroyItem(dataStore.getValue(childItem, "id"));
				});
				dataStore.deleteItem(item);
			}else{
				dojo.xhrPost({
					url:"ajax/deleteProject.php",
					postData:"id="+dbkey,
					load:function(response, ioArgs){
						if(response=="deleted"){
							writeToStatusBar("Deleted", 5000);
							dataStore.deleteItem(item);
						}else{
							writeToStatusBar("Failed to delete", 5000);
						}
						
						completeCallback();
					},
					error:function(error,ioArgs){
						writeToStatusBar(error, 5000);
						errorCallback();
					}
				});
			}
		}
	});
	dataStore = null;
	tree.destroyRecursive();
}

//rename the project
function renameProject(){
	dataStore.fetchItemByIdentity({
		identity: "project",
		onItem: function(item){
			var name = prompt("Enter New Name",dataStore.getValue(item,"name"));
			if(name!=undefined && name!=null && name!=""){
				dataStore.setValue(item,"name",name);
			}
		}
	});
}

//display the upload form
function uploadProject(){
	var tab = new dojox.layout.ContentPane({
		id: "uploadTab",
		title:"Upload File",
		href:'ajax/uploadFile.php',
		closable:true
	});
	dijit.byId("tabContainer").addChild(tab);
	dijit.byId("tabContainer").selectChild(tab);
}

//download the current project
function downloadProject(){
	if(dataStore){
		dataStore.fetchItemByIdentity({
			identity: "project",
			scope: this,
			onItem: function(item){
				var id = dataStore.getValue(item, "db_key");
				if(id>0){
					window.open("ajax/download.php?id=" + id);
				}else{
					alert("Please save the project first.");
				}
			}
		});
	}
}

//show help: uml resources
function showUMLResources(){
	var tab;
	if(!dijit.byId('help')){
		tab = new dijit.layout.ContentPane({
			id:'help',
			title:"Help Contents",
			closable:true,
			content:'<iframe id="helpIframe" src="ajax/help/index.html#resources" style="border:0px solid black;width:100%;height:95%;"><p>Your browser does not support iframes.</p></iframe>'
		});
		dijit.byId("tabContainer").addChild(tab);
	}else{
		tab=dijit.byId('help');
		dojo.byId("helpIframe").src="ajax/help/uml_resources.html";
	}
	dijit.byId("tabContainer").selectChild(tab);
}

//show help: getting started
function showGettingStarted(){
	var tab;
	if(!dijit.byId('help')){
		tab = new dijit.layout.ContentPane({
			id:'help',
			title:"Help Contents",
			closable:true,
			content:'<iframe id="helpIframe" src="ajax/help/getting_started.html" style="border:0px solid black;width:100%;height:95%;"><p>Your browser does not support iframes.</p></iframe>'
		});
		dijit.byId("tabContainer").addChild(tab);
	}else{
		tab=dijit.byId('help');
		dojo.byId("helpIframe").src="ajax/help/getting_started.html";
	}
	dijit.byId("tabContainer").selectChild(tab);
}

//show help
function showHelp(){
	var tab;
	if(!dijit.byId('help')){
		tab = new dijit.layout.ContentPane({
			id:'help',
			title:"Help Contents",
			closable:true,
			content:'<iframe id="helpIframe" src="ajax/help/index.html" style="border:0px solid black;width:100%;height:95%;"><p>Your browser does not support iframes.</p></iframe>'
		});
		dijit.byId("tabContainer").addChild(tab);
	}else{
		tab=dijit.byId('help');
		dojo.byId("helpIframe").src="ajax/help/index.html";
	}
	dijit.byId("tabContainer").selectChild(tab);
}

//show about dialog box
function showAbout(){
	var dialogId = createPopUpDialog("About UML WebStudio", "<p style=\"text-align:justify\">A special problem of Robenie D. Fernandez and Prof. Rodolfo N. Duldulao Jr.</p>");
	var dialog = dijit.byId(dialogId);
	dialog.show();
}

